package com.example.yydm666.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yydm666.entity.Cart;
import com.example.yydm666.entity.Cart;
import com.example.yydm666.entity.Orders;
import com.example.yydm666.entity.User;
import com.example.yydm666.service.CartService;
import com.example.yydm666.service.UserAddressService;
import io.swagger.annotations.Api;
import org.apache.catalina.connector.Request;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-03-28
 */
@Controller
@RequestMapping("/cart")
@Api(tags = "购物车模块")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private UserAddressService userAddressService;

    /*
     * 添加购物车对库存有两层判断前端js一层如果库层为0弹出库存不足
     *service一层库存为0Slf4j弹出失败信息,双层检测
     * */

    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(@PathVariable("productId") Integer productId,
                      @PathVariable("price") Float price,
                      @PathVariable("quantity") Integer quantity,
                      HttpSession session
    ) {
        Cart cart = new Cart();
        cart.setProductId(productId);
        cart.setQuantity(quantity);
        cart.setCost(price * quantity);
//        获取的时登录的id所有得先登录l,l,
        User user = (User) session.getAttribute("user");
        cart.setUserId(user.getId());
        try {
            if (cartService.save(cart)) {
                return "redirect:/cart/findAllCart";
            }
        } catch (Exception e) {
            return "redirect:/productCategory/main";
        }
        return null;
    }

    //    购物车商品信息
    @GetMapping("/findAllCart")
    public ModelAndView findAllCart(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement1");
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("list", cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }

    //    购物车商品删除操作
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        cartService.removeById(id);
        return "redirect:/cart/findAllCart";
    }

    //    确认结算
    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement2");
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", user.getId());
        modelAndView.addObject("addressList", userAddressService.list(queryWrapper));
        return modelAndView;
    }

    @GetMapping("/update/{id}/{quantity}/{cost}")
    @ResponseBody
    public String update(@PathVariable("id") Integer id,
                         @PathVariable("quantity") Integer quantity,
                         @PathVariable("cost") Float cost) {
        Cart cart = cartService.getById(id);
        cart.setQuantity(quantity);
        cart.setCost(cost);
        if (cartService.updateById(cart)) {
            return "success";
        } else {
            return "fail";
        }
    }

}
